<?php

declare(strict_types=1);

namespace app\controller\sys;

use think\facade\View;
use think\facade\Session;

use app\model\SysMenu;
use app\model\SysUser;
use app\model\SysRole as SysRoleModel;
use app\utils\ReUtils;

class SysRole extends Base
{
    protected $modelName="SysRole";

    public function index()
    {
        $list = SysRoleModel::field('roleid,name,permissions,remark,create_time')->select();

        View::assign('list', $list);

        return View::fetch();
    }

    public function save($id = 0)
    {
        if ($this->app->request->isPost()) {
            $params = $this->app->request->param();
            if ($params['name'] == '') {
                $this->error("目录名不能为空");
            }

            $permissions = '';
            if (!empty($params['perm_check'])) {
                $perm_checks = $params['perm_check'];
                $permissions = implode(',', $perm_checks);
            }
            unset($params['perm_check']);
            $params['permissions'] = $permissions;

            try {
                $id = $params['roleid']; 
                unset($params['roleid']);
    
                if ($id != 0) {
                    SysRoleModel::update($params, ['roleid' => $id]);
                } else {
                    SysRoleModel::create($params);
                }
            } catch (\Exception $e) {
                $msg = $e->getMessage();

                $this->error("错误提示:".$msg);
            }
            $this->success('操作成功', 'sys_role/index');
        } else {
            if ($id != 0) {
                $data = SysRoleModel::find($id);
                // 已有权限数组
                $rolePowers = explode(',', $data->permissions);
            } else {
                $data = new SysRoleModel();
                $rolePowers = [];
            }
    
            $dataPermission = list_tree(sysMenu::select(), 'menuid');
    
            return View::fetch('save', [
                'data' => $data,
                'rolePowers' => $rolePowers,
                'dataPermission' => $dataPermission
            ]);
        }
    }

    public function delete($id = null)
    {
        if ($this->app->request->isAjax()) {
            $U_role_id = SysUser::where('userid', Session::get('adminuser.roleid'))->value('roleid');

            if (is_array($id)) {
                if (in_array('1', $id)) {
                    return ReUtils::error('超级管理员无法删除');
                }
                if (in_array($U_role_id, $id)) {
                    return ReUtils::error('当前登录用户角色无法删除');
                }
            } else {
                if ($id == 1) {
                    return ReUtils::error('超级管理员无法删除');
                }
                if ($id == $U_role_id) {
                    return ReUtils::error('当前登录用户角色无法删除');
                }
            }
            if (SysRoleModel::destroy($id)) {
                return ReUtils::success();
            } else {
                return ReUtils::error();
            }
        }
    }
}
